<template>
    <div>
       <Row type="flex" justify="space-between" align="middle">
            <Col span="15" class="title">角色列表</Col>
            <Col span="9" :style="{textAlign: 'right'}">
                <Button icon="md-add" @click="addRole">添加角色</Button>
            </Col>
       </Row>
       <div class="table-box">
           <Table width="100%" :height="height" border :columns="columns" :data="data" :loading="loading"></Table>
       </div>
    </div>
</template>
<script>
    export default {
        data () {
            var height = this.$util.getClientHeight()-64-51-30-58-15;
            var that = this;

            return {
                loading: true,
                height: height,
                columns: [
                    {
                        title: 'ID',
                        key: 'id',
                        width: 70
                    },
                    {
                        title: '名称',
                        key: 'name',
                        width: 150
                    },
                    {
                        title: '创建者',
                        key: 'creator_name',
                        width: 150
                    },
                    {
                        title: '创建时间',
                        key: 'create_time',
                        width: 170
                    },
                    {
                        title: '描述',
                        key: 'description',
                        width: 200
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 200,
                        render: (h, params) => {
                            var flag = params.row.flag ? 0 : 1;
                            var buttonAction = [
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: ()=>{
                                                that.editRole(params);
                                            }
                                        }
                                    }, '编辑'),
                                    h('Button', {
                                        props: {
                                            type: 'error',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: ()=>{
                                                that.delRole(params.row);
                                            }
                                        }
                                    }, '删除'),
                                    h('Button', {
                                        props: {
                                            type: 'default',
                                            size: 'small'
                                        },
                                        on: {
                                            click: ()=>{
                                                that.$loading.show();
                                                that.assignPermissions(params.row);
                                            }
                                        }
                                    }, '分配权限')
                                ];

                            return h('div', buttonAction);
                        }
                    }
                ],
                data: []
            }
        },
        created: function () {
            // 获取菜单列表信息
            this.getRoleList();
        },
        methods: {
            // 获取菜单信息列表
            getRoleList(){
                var that = this;
                that.$util.ajax({
                    that: that,
                    url: '/role/rolelist/',
                    success: function(data){
                        that.data = data.data;
                        that.loading = false;
                    },
                    fail: function(msg){
                        that.$Message.info(msg);
                    }
                })
            },
            // 添加角色组
            addRole(){
                this.$router.push({
                                    path: '/admin/addRole',
                                    params: {}
                                });
            },
            // 编辑角色组
            editRole(params){
                this.$router.push({
                                    path: '/admin/editRole',
                                    query: params.row
                                });
            },
            // 删除角色组
            delRole(params){
                var that = this;
                that.$loading.show('更新中');
                that.$util.ajax({
                    that: that,
                    url: '/role/del/',
                    data: {
                        id: params.id
                    },
                    success: function(data){
                        that.$Message.info('删除成功！');
                        that.getRoleList();
                    },
                    fail: function(msg){
                        that.$Message.info(msg);
                    },
                    complete: function(){
                        that.$loading.hide();
                    }
                })
            },
            // 分配权限
            assignPermissions(params){
                this.$router.push({
                                    path: '/admin/assignPermissions',
                                    query: params
                                });
            }
        }
    }
</script>
<style scoped>

</style>